/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import dao.UsuarioDao;
import daoimp.UsuarioDaoImp;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import modelo.dominio.Menu;
import modelo.dominio.Usuario;

/**
 *
 * @author Gera
 */
public class AltaUsuario extends javax.swing.JDialog {
    ArrayList<Usuario> listaUsu = new ArrayList<Usuario>();
    UsuarioDao usuDao = new UsuarioDaoImp();
    Usuario usuario = new Usuario();
    /**
     * Creates new form AltaUsuario
     */
    public AltaUsuario(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        this.txtIdUsu.setText("Autogenerable");
        this.txtIdUsu.setEnabled(false);
        this.btnModiUsu.setEnabled(false);
    }

    public AltaUsuario(java.awt.Frame parent, boolean modal, Usuario u) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        usuario = u;
        this.txtIdUsu.setText(String.valueOf(usuario.getIdUsuario()));
        this.txtIdUsu.setEnabled(false);
        this.btnGuardarUsu.setEnabled(false);
        this.cargarAlta();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtIdUsu = new javax.swing.JTextField();
        txtUsuario = new javax.swing.JTextField();
        cmbRolUsu = new javax.swing.JComboBox();
        btnGuardarUsu = new javax.swing.JButton();
        btnModiUsu = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        txtPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Id:");

        jLabel2.setText("Usuario:");

        jLabel3.setText("Password:");

        jLabel4.setText("Rol:");

        cmbRolUsu.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administrador", "Empleado" }));

        btnGuardarUsu.setText("Guardar");
        btnGuardarUsu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarUsuActionPerformed(evt);
            }
        });

        btnModiUsu.setText("Modificar");
        btnModiUsu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModiUsuActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(btnGuardarUsu)
                        .addGap(28, 28, 28)
                        .addComponent(btnModiUsu)
                        .addGap(27, 27, 27)
                        .addComponent(btnCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtUsuario)
                            .addComponent(cmbRolUsu, 0, 197, Short.MAX_VALUE)
                            .addComponent(txtIdUsu)
                            .addComponent(txtPassword))))
                .addContainerGap(46, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtIdUsu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cmbRolUsu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardarUsu)
                    .addComponent(btnModiUsu)
                    .addComponent(btnCancelar))
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGuardarUsuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarUsuActionPerformed
        // TODO add your handling code here:
        listaUsu = usuDao.getAll();
        Usuario usua = new Usuario();
        for (int x=0; x<=listaUsu.size(); x++){
            if (this.txtUsuario.getText().equals(listaUsu.get(x).getUsuario())){
                JOptionPane.showMessageDialog(this, "Usuario Existente");
                break;
            }
            else{
                usua.setUsuario(this.txtUsuario.getText());
                usua.setPassword(this.txtPassword.getText());
                usua.setRol(this.cmbRolUsu.getSelectedItem());
                usuDao.insert(usua);
                this.dispose();
                break;
            }
        }        
    }//GEN-LAST:event_btnGuardarUsuActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnModiUsuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModiUsuActionPerformed
        // TODO add your handling code here:
        this.modificar();
    }//GEN-LAST:event_btnModiUsuActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AltaUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AltaUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AltaUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AltaUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                AltaUsuario dialog = new AltaUsuario(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardarUsu;
    private javax.swing.JButton btnModiUsu;
    private javax.swing.JComboBox cmbRolUsu;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtIdUsu;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
    private void cargarAlta() {
        this.txtIdUsu.setText(String.valueOf(usuario.getIdUsuario()));
        this.txtUsuario.setText(usuario.getUsuario());
        this.txtPassword.setText(String.valueOf(usuario.getPassword()));
        this.cmbRolUsu.addItem(usuario.getRol());
    }

    private void modificar() {
        Usuario us = new Usuario();
        usuario.setUsuario(this.txtUsuario.getText());
        usuario.setPassword(this.txtPassword.getText());
        us=usuario;
        usuDao.update(us);
        this.dispose();
       
    }
}
